<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>设计链表--单链表</title>
</head>
<body>
<script type="text/javascript" src="./js/LinkedList1.js"></script>
<!-- <script>
var ListNode = function(val) {
    this.val = val;
    this.next = null;
};

var LinkedList = function() {
    this.head = null;
    this.length = 0;
};

/**
 * 获取链表中第 index 个节点的值。如果索引无效，则返回-1。
 * @param {number} index
 * @return {number}
 */
LinkedList.prototype.get = function(index) {
	if(index < 0 || index > this.length -1) {
		return -1;
	}
	let curHead = this.head;

	while(curHead) {
		if(index == 0) {
			return curHead.val;
		}
		index--;
		curHead = curHead.next;
	}

};

/**
 * addAtHead(val)：在链表的第一个元素之前添加一个值为 val 的节点。插入后，新节点将成为链表的第一个节点。
 * @param {number} val
 * @return {void}
 */
LinkedList.prototype.addAtHead = function(val) {
	let newHead = new ListNode(val);
	let curHead = this.head;
	this.head = newHead;
	this.head.next = curHead;
	this.length++;
};

/**
 * addAtTail(val)：将值为 val 的节点追加到链表的最后一个元素。
 * @param {number} val
 * @return {void}
 */
LinkedList.prototype.addAtTail = function(val) {
	let newHead = new ListNode(val);
	let curHead = this.head;

	while(curHead.next) {
		curHead = curHead.next;
	}
	
	curHead.next = newHead;
	this.length++;
};

/**
 * addAtIndex(index,val)：在链表中的第 index 个节点之前添加值为 val  的节点。
 * 如果 index 等于链表的长度，则该节点将附加到链表的末尾。
 * 如果 index 大于链表长度，则不会插入节点。
 * 如果index小于0，则在头部插入节点。
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
LinkedList.prototype.addAtIndex = function(index, val) {
	if(index > this.length -1) {
		return;
	}
	if(index <= 0) {
		return this.addAtHead(val);
	}
	if(index == this.length) {
		return this.addAtTail(val);
	}

	let newHead = new ListNode(val);
	let curHead = this.head;

	while(index > 1) {
		index--;
		curHead = curHead.next;
	}

	newHead.next = curHead.next;
	curHead.next = newHead;

	this.length++;
};

/**
 * 如果索引 index 有效，则删除链表中的第 index 个节点。
 * @param {number} index
 * @return {void}
 */
LinkedList.prototype.deleteAtIndex = function(index) {
	if(index < 0 || index > this.length -1) {
		return;
	}
	// 删除第1个
	if(index == 0) {
		this.head = this.head.next;
		this.length--;
		return;
	}
	let curHead = this.head;

	while(index > 1) {
		curHead = curHead.next;
		index--;
	}

	if(curHead.next.next) {
		curHead.next = curHead.next.next;
	}else {
		curHead.next = null;
	}
	this.length--;
};
</script> -->
<script type="text/javascript">
var obj = new LinkedList()
//var param_1 = obj.get(1)
obj.addAtHead(12)
obj.addAtHead(11)
obj.addAtTail(13)
obj.addAtTail(14)
//obj.addAtIndex(1,333)
//obj.deleteAtIndex(444)
obj.deleteAtIndex(4)
console.log(obj);
console.log(obj.get(2));
//console.log(param_1);
</script>
</body>
</html>